package alogrithm;

/**
 * @author lbw
 * @version 1.0
 * @date 2021/10/18 12:14
 */
public class LongestCommonPrefix {
    public String longestCommonPrefix(String[] strs) {
        // write code here
        int n = strs.length;
        if (n == 0) {
            return "";
        }
        if (n == 1) {
            return strs[0];
        }
        String res = strs[0];
        for (int i = 1; i < n; ++i) {
            res = findCommonPerfix(strs[i], res);
        }
        return res;
    }

    public String findCommonPerfix(String s1, String s2) {
        int i = 0;
        int n = Math.min(s1.length(), s2.length());
        while (i < n && s1.charAt(i) == s2.charAt(i)) {
            i++;
        }
        return s1.substring(0, i);
    }
}
